#include "stdafx.h"
#include "wf_infodist2.h"

#include "pf_snapper.h"



WF_InfoDist2::WF_InfoDist2(PF_Container& container,
                                       PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Info Dist2",
                           container, graphicView) {}


QAction* WF_InfoDist2::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// (tr("Distance Entity to Point")
    QAction* action = new QAction(tr("&Distance Entity to Point"), NULL);
    //action->zetStatusTip(tr("Measures the distance between an entity and a point"));
	action->setIcon(QIcon(":/extui/infodist2.png"));
    return action;
}

void WF_InfoDist2::init(int status) {
    PF_ActionInterface::init(status);
}



void WF_InfoDist2::trigger() {

    PF_DEBUG->print("PF_ActionInfoDist2::trigger()");

    if (point.valid && entity!=NULL) {
        double dist = entity->getDistanceToPoint(point);
        QString str;
        str.sprintf("%.6f", dist);
        PF_DIALOGFACTORY->commandMessage(tr("Distance: %1").arg(str));
    }
}



void WF_InfoDist2::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionInfoDist2::mouseMoveEvent begin");

    switch (getStatus()) {
    case SetEntity:
        //entity = catchEntity(e);
        break;

    case SetPoint:
        if (entity!=NULL) {
            point = snapPoint(e);
        }
        break;

    default:
        break;
    }

    PF_DEBUG->print("PF_ActionInfoDist2::mouseMoveEvent end");
}



void WF_InfoDist2::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {

        switch (getStatus()) {
        case SetEntity:
            entity = catchEntity(e);
            if (entity!=NULL) {
                setStatus(SetPoint);
            }
            break;

        case SetPoint: {
                PF_CoordinateEvent ce(snapPoint(e));
                coordinateEvent(&ce);
            }
            break;

        default:
            break;
        }
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}


void WF_InfoDist2::coordinateEvent(PF_CoordinateEvent* e) {
    if (e==NULL) {
        return;
    }

    if (getStatus()==SetPoint && entity!=NULL) {
        point = e->getCoordinate();
        graphicView->moveRelativeZero(point);
        trigger();
        setStatus(SetEntity);
    }
}



void WF_InfoDist2::updateMouseButtonHints() {
    switch (getStatus()) {
    case SetEntity:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify entity"),
            tr("Cancel"));
        break;
    case SetPoint:
        PF_DIALOGFACTORY->updateMouseWidget(
            tr("Specify point"),
            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_InfoDist2::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_InfoDist2::updateToolBar() {
    switch (getStatus()) {
    case SetPoint:
    case SetEntity:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        break;
    default:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarInfo);
        break;
    }
}


// EOF
